package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.byxm.wlgw.bean.SpppBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.query.SpppQuery;
/**
 *   商品品牌信息
 * */
public class SpppService {
	SpppQuery spppQuery=new SpppQuery("tb_yhxx");
	public List<SpppBean> getSppp(int splbfid) throws Exception{
		String sql = spppQuery.getSpppSql(splbfid);
		DataBaseAccess dba=null;
		List<SpppBean> list=new ArrayList<SpppBean>();
		try {
			dba = new DataBaseAccess();
			ResultSet rs = dba.executeQuery(sql);
			while (rs.next()) {	
				SpppBean sppp=new SpppBean();
				sppp.setSpppid(rs.getInt("spppid"));
				sppp.setSpppm(rs.getString("spppm"));
				sppp.setSplbfid(rs.getInt("splbfid"));
				sppp.setPx(rs.getInt("px"));
				list.add(sppp);
				}
			rs.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return list;
	}
	
	
	public boolean saveSppp(SpppBean spppBean) throws Exception{
		DataBaseAccess dba=null;
		PreparedStatement pstmt = null;
		int iflag=0;
		try {
			dba = new DataBaseAccess();
			pstmt=dba.getPstmt(spppQuery.getSaveSql());
			pstmt.setString(1, spppBean.getSpppm());
			pstmt.setInt(2,spppBean.getSplbfid());
			pstmt.setInt(3,spppBean.getPx());
			iflag=pstmt.executeUpdate();
			dba.commit();
			pstmt.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(iflag>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 *通过品牌名称进行查找，看看有木有重复的品牌
	 * @param spppm
	 * @return
	 * @throws Exception
	 */
	public SpppBean findSpppBySpppm(String spppm) throws Exception{
		DataBaseAccess dba=null;
		ResultSet rs=null;
		SpppBean spppBean=null;
		try {
			dba=new DataBaseAccess();
			rs=dba.executeQuery(spppQuery.findSpppBySpppm(spppm));
			if(rs.next()){
				spppBean=new SpppBean();
				spppBean.setSpppid(rs.getInt("spppid"));
				spppBean.setSpppm(rs.getString("spppm"));
				spppBean.setPx(rs.getInt("px"));
				spppBean.setSplbfidvar(rs.getString("splbfidvar"));
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return spppBean;
	}
	/**
	 * update商品品牌
	 * @throws Exception 
	 */
	public void updateSppp(SpppBean spppBean) throws Exception{
		DataBaseAccess dba=null;
		PreparedStatement ps=null;
		int iFlag=0;
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(spppQuery.updateSppp());
			ps.setString(1,spppBean.getSplbfidvar() );
			ps.setInt(2, spppBean.getSpppid());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e1) {
			dba.rollback();
			e1.printStackTrace();
		}finally{
			dba.releaseConnect();
		}

	}

	
	
	

}
